package com.jie.deletefile.Doahang;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RadioButton;

import java.util.List;

/**
 * Created by Administrator on 2016/11/28 0028.
 */

/**
 * 本类用于设置Fragment,RadioButton,viewpager一起使用
 * 用法: 初始化类,调用show()方法
 *
 * 构造方法参数
 *   context
 *   list<Fragment>
 *   RadioButton[]
 *   ViewPager
 *   count  条目个数
 *   Imageview 实现viewpager滑动时的动画效果
 */
public class VpFrag {
    private List<Fragment> fragments;
    private RadioButton[] radioButtons;
    private Context context;
    private ViewPager pager;
    private int count;
    private float width;
    private float startOffset;
    private float stopOffset;
    private ImageView view;


    public VpFrag(Context context, List<Fragment> fragments, RadioButton[] radioButtons, ViewPager pager, int count, ImageView view) {
        this.context = context;
        this.fragments = fragments;
        this.radioButtons = radioButtons;
        this.pager = pager;
        this.count = count;
        this.view = view;
    }

    public void show() {

        initsize();
        initAnim();
        initclick();

    }

    /**
     * 每个RadioButton的点击事件,更新imageview的位置和viewpager
     */
    private void initclick() {
        for (int i= 0 ;i<radioButtons.length;i++) {
            final int finalI = i;
            radioButtons[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    pager.setCurrentItem(finalI);
                    stopOffset = width / count * finalI;
                    new ObjectAnimator().ofFloat(view, "translationX", startOffset, stopOffset).setDuration(200).start();

                }
            });
        }
    }

    /**
     * 设置每个radiobutton 都未被选中
     */
    private void setRadioButtons() {
        for (int i = 0; i < radioButtons.length; i++) {
            radioButtons[i].setChecked(false);
        }
    }

    /**
     * viewpager滑动式的动画效果和radiobutton的选择
     */
    private void initAnim() {
        radioButtons[0].setChecked(true);
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                startOffset = (width * position)/count;
                stopOffset = startOffset + positionOffsetPixels/count;
                startOffset = stopOffset;
                Log.e("TAG", "onPageScrolled: start" + startOffset);
                Log.e("TAG", "onPageScrolled: stop"+stopOffset);
                Log.e("TAG", "onPageScrolled: postion"+position );
                new ObjectAnimator().ofFloat(view, "translationX", startOffset, stopOffset).setDuration(200).start();
            }

            @Override
            public void onPageSelected(int position) {
                setRadioButtons();
                radioButtons[position].setChecked(true);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 获取屏幕宽度,设置imageview的width
     */
    private void initsize() {
        DisplayMetrics metrics = new DisplayMetrics();

        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        manager.getDefaultDisplay().getMetrics(metrics);
        width = metrics.widthPixels;
//        view.setMaxWidth((int) (width/count));
//        view.setMinimumWidth((int) (width/count));
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.width = (int) (width / count);
        view.setLayoutParams(params);
    }
}
